﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using System.IO;
using Common;

public partial class shopping_cartutility : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;

        switch (RequestBase.GetFormString("otype"))
        {
            case "addtocart":
                AddCard();
                break; 
            case "loadingcartitems":
                LoadingCartItems();
                break;
            case "loadingminicartitems":
                LoadingMiniCartItems();
                break;
            case "loadingcartitemsview":
                LoadingCartItemsView();
                break;
            case "updateitemqty":
                UpdateQty();
                break;
            case "removecartitem":
                RemoveItem();
                break; 
            case "clearcartitems":
                ClearCartItems();
                break;
            case "totalcartamount":
                TotalCartAmount();
                break;
        } 
    }


    #region 购物车方法
    /// <summary>
    /// 添加购物车
    /// </summary>
    void AddCard()
    {
        if (!string.IsNullOrEmpty(this.User.Identity.Name))
        {
            if (Utils.IsGuid(RequestBase.GetFormString("_itemid")) && Utils.IsNumber(RequestBase.GetFormString("qty")))
            {
                Profile.CardItems.Add(RequestBase.GetFormString("_itemid"), int.Parse(RequestBase.GetFormString("qty")));
                Profile.Save(); 

                BLL.AjaxHelper.ResponseWrite("succeeded");
            }
            else
            {
                BLL.AjaxHelper.ResponseWrite("faild");
            }
        }
        else
            BLL.AjaxHelper.ResponseWrite("login");
    }

    void LoadingCartItems()
    {
        try
        {
            if (!string.IsNullOrEmpty(this.User.Identity.Name))
            {
                string ctrl = "~/_controls/ctlCartItems.ascx";
                Control uc_rep = LoadControl(ctrl);
                Repeater rptCartItems = (Repeater)uc_rep.FindControl("rptCardItems");

                DataTable dat = BLL.CommonBLL.Select("vw_Profiles_Items", "*", " AND Username = '" + this.User.Identity.Name + "' AND ProfileName = 'CardItems' ORDER BY ProductNumber ASC");
                rptCartItems.DataSource = dat;
                rptCartItems.DataBind();

                StringBuilder strb = new StringBuilder();
                StringWriter sw = new StringWriter(strb);
                HtmlTextWriter htw = new HtmlTextWriter(sw);

                rptCartItems.RenderControl(htw);

                BLL.AjaxHelper.ResponseWrite(strb.ToString());
            }
            else
                BLL.AjaxHelper.ResponseWrite("login");
        }
        catch (System.Threading.ThreadAbortException err)
        {
            //nothing   to   do   
        }
        catch (Exception err)
        {
            BLL.AjaxHelper.ResponseWrite(err.Message);
        }
    }

    void LoadingMiniCartItems()
    {
        try
        {
            if (!string.IsNullOrEmpty(this.User.Identity.Name))
            {
                string ctrl = "~/_controls/ctlCartItems.ascx";
                Control uc_rep = LoadControl(ctrl);
                Repeater rptMiniCartItems = (Repeater)uc_rep.FindControl("rptMiniCartItems");

                DataTable dat = BLL.CommonBLL.Select("vw_Profiles_Items", "*", " AND Username = '" + this.User.Identity.Name + "' AND ProfileName = 'CardItems' ORDER BY ProductNumber ASC");
                rptMiniCartItems.DataSource = dat;
                rptMiniCartItems.DataBind();

                uc_rep.GetType().GetProperty("TotalInfo").SetValue(uc_rep, "购物车内共" + Profile.CardItems.Count.ToString() + "件商品<span>总计:<em>￥" + Profile.CardItems.TotalCost.ToString() + "</em></span>", null);

                StringBuilder strb = new StringBuilder();
                StringWriter sw = new StringWriter(strb);
                HtmlTextWriter htw = new HtmlTextWriter(sw);

                rptMiniCartItems.RenderControl(htw);

                BLL.AjaxHelper.ResponseWrite(strb.ToString());
            }
            else
                BLL.AjaxHelper.ResponseWrite("login");
        }
        catch (System.Threading.ThreadAbortException err)
        {
            //nothing   to   do   
        }
        catch (Exception err)
        {
            BLL.AjaxHelper.ResponseWrite(err.Message);
        }
    }

    void LoadingCartItemsView()
    {
        try
        {
            if (!string.IsNullOrEmpty(this.User.Identity.Name))
            {
                string ctrl = "~/_controls/ctlCartItems.ascx";
                Control uc_rep = LoadControl(ctrl);
                Repeater rptCardItemsView = (Repeater)uc_rep.FindControl("rptCardItemsView");

                DataTable dat = BLL.CommonBLL.Select("vw_Profiles_Items", "*", " AND Username = '" + this.User.Identity.Name + "' AND ProfileName = 'CardItems' ORDER BY ProductNumber ASC");
                rptCardItemsView.DataSource = dat;
                rptCardItemsView.DataBind();

                StringBuilder strb = new StringBuilder();
                StringWriter sw = new StringWriter(strb);
                HtmlTextWriter htw = new HtmlTextWriter(sw);

                rptCardItemsView.RenderControl(htw);

                BLL.AjaxHelper.ResponseWrite(strb.ToString());
            }
            else
                BLL.AjaxHelper.ResponseWrite("login");
        }
        catch (System.Threading.ThreadAbortException err)
        {
            //nothing   to   do   
        }
        catch (Exception err)
        {
            BLL.AjaxHelper.ResponseWrite(err.Message);
        }
    }

    void UpdateQty()
    {
        if (!string.IsNullOrEmpty(this.User.Identity.Name))
        {
            if (Utils.IsGuid(RequestBase.GetFormString("_itemid")) && Utils.IsNumber(RequestBase.GetFormString("qty")))
            {
                Profile.CardItems.SetQuantity(RequestBase.GetFormString("_itemid"), int.Parse(RequestBase.GetFormString("qty")));
                Profile.Save();

                BLL.AjaxHelper.ResponseWrite("succeeded");
            }
            else
            {
                BLL.AjaxHelper.ResponseWrite("请输入数值型数据！");
            }
        }
        else
            BLL.AjaxHelper.ResponseWrite("请先登录再执行此操作！");
    }

    void RemoveItem()
    {
        if (!string.IsNullOrEmpty(this.User.Identity.Name))
        {
            if (Utils.IsGuid(RequestBase.GetFormString("_itemid")))
            {
                Profile.CardItems.Remove(RequestBase.GetFormString("_itemid"));
                Profile.Save();

                BLL.AjaxHelper.ResponseWrite("succeeded");
            }
            else
            {
                BLL.AjaxHelper.ResponseWrite("参数错误，请重试！");
            }
        }
        else
            BLL.AjaxHelper.ResponseWrite("请先登录再执行此操作！");
    }

    void TotalCartAmount()
    {
        decimal total = Profile.CardItems.TotalCost;

        BLL.AjaxHelper.ResponseWrite(total.ToString());
    }
    void ClearCartItems()
    {
        if (!string.IsNullOrEmpty(this.User.Identity.Name))
        {
            if (Profile.CardItems.Count > 0)
            {
                Profile.CardItems.Clear();
                Profile.Save();
            }

            BLL.AjaxHelper.ResponseWrite("succeeded");
        }
        else
            BLL.AjaxHelper.ResponseWrite("请先登录再执行此操作！");
    }
    #endregion 
}